<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newForum
 *
 * @author ivan
 */
include_once 'function.php';


class newTopic {
    //put your code here
    
    
    private $topic;
    private $contentOfReply;
    private $Db;
    private $topicID;
    private $forumTitle;
    private $forumId1;
    private $lastUpdatedBy;
    
    
    public function __construct($topic,$comment,$forumId1) {
        
        $topic = htmlspecialchars($topic);
        $comment = htmlspecialchars($comment);
        $forumId1 = htmlspecialchars($forumId1);
        
        
        $this->topic = $topic;
        $this->contentOfReply = $comment;
        $this->forumId1 = $forumId1;
        $this->Db = Db();
        
    }
    
    
    public function check(){
        
        if(strlen($this->topic) < 5){
            
            $error = "Topic length too short";
            return $error;
        }
        else{
            
            if(strlen($this->contentOfReply) > 0){
                
                $value = $this->checkexistance();
                
                if($value==1){
                    return "ok";
                }
                else{
                    
                    $error = "A topic with this name already exists, please choose another";
                    return $error;
                }
                
            }
            else{
                
                $error = "You must comment your first topic";
                return $error;
            }
            
        }
        
    }
    
    
    
    public function checkexistance(){
        
        $query = $this->Db->prepare('SELECT * FROM topic where topicTitle = :topic');
        $query->execute(array(
            'topic' => $this->topic,
        ));
        
        $response = $query->fetch();
        
        if ($response == FALSE){
            
            return 1;
        }
        
        else{
            
            return -1;
        }
        
    }
    
    
    
    public function add(){
        
         $query = $this->Db->prepare('INSERT INTO topic(topicTitle,dateCreated,numberOfReplies,forumID,owner,lastUpdatedBy,dateUpdated) values(:topic,NOW(),0,:forumId1,:owner,:lastUpdate,NOW())');
         $query->execute(array(
            'topic' => $this->topic,
             'owner' => $_SESSION['id'],
             'forumId1' => $this->forumId1,
             'lastUpdate' => $_SESSION['id'],
             
        ));
         
       
         
        $this->update();
         
         
         
         $query = $this->Db->prepare('SELECT * FROM topic where topicTitle = :topic');
            $query->execute(array(
            'topic' => $this->topic,
        ));
            
           $response = $query->fetch();
           
           $this->topicID = $response['tid'];
         
         
          $query = $this->Db->prepare('update topic SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateCreated = NOW() where tid = :topicID');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'topicID' => $this->topicID,     
        ));
         
           
         
         
         $query = $this->Db->prepare('INSERT INTO reply(contentOfReply,dateCreated,dateUpdated,topicID,owner) values(:contentOfReply,CURDATE(),CURDATE(),:topicID,:owner)');
         $query->execute(array(
             'contentOfReply' => $this->contentOfReply,
             'owner' => $_SESSION['id'],
             'topicID' => $this->topicID,
        ));
         
         $this->updateReplies();
       
        return 1;
    }
    
    public function update(){
        
             
        $query = $this->Db->prepare('update forum SET numberOfTopics = numberOfTopics + 1,lastUpdatedBy = :lastUpdatedBy, dateCreated = NOW() where fid = :forumId1');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'forumId1' => $this->forumId1,     
        ));
        
        
    }
    
    public function updateReplies(){
        
             
        $query = $this->Db->prepare('update forum SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateCreated = NOW() where fid = :forumId1');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'forumId1' => $this->forumId1,     
        ));
        
        
    }
    
    public function deleteTopic($topicID, $forumID){
        $topicQuery = mysql_query("SELECT * FROM topic");
        if (mysql_num_rows($topicQuery) > 0) {
                while ($row = mysql_fetch_assoc($topicQuery)) {
                    $updateTopicQuery = mysql_query("UPDATE topic SET isDeleted = t WHERE ". $_GET['tid']." = topicID");
                    $updateForumQuery = mysql_query("UPDATE forum SET numberOfTopics = numberOfTopics-1 WHERE ". $_GET['fid']." = forumID1");
                    $temp = mysql_query("SELECT numberOfReplies FROM topic WHERE ". $_GET['tid']."= topicID");
                    $updateForumQuery2 = mysql_query("UPDATE forum SET numberOfReplies = ". $temp."-1 WHERE ". $_GET['fid']." = forumID1");
                    
         }
         #keeping these around just in case
        #set the topic to deleted
        //$query = $this->Db->prepare('UPDATE topic SET isDeleted = true WHERE tid = topicID');
         //update the # topics in the forum
        //$query = $this->Db->prepare('update forum SET numberOfTopics = numberOfTopics - 1 WHERE fid = :forumId1');
        //update the #replies in the forum
        //$query = $this->Db->prepare('update forum SET numberOfReplies = numberOfReplies + 1 where fid = :forumId1');
        }
    }
    
}
